package com.buer.storage.service.impl;

import com.buer.storage.dao.AorderDao;
import com.buer.storage.dao.DorderDao;
import com.buer.storage.dao.StockDao;
import com.buer.storage.dto.*;
import com.buer.storage.entity.*;
import com.buer.storage.service.AorderService;
import com.buer.storage.utils.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AorderServiceImpl implements AorderService {
    private SnowFlake snowFlake=new SnowFlake(2,3);
    @Resource
    private AorderDao aorderDao;
    @Resource
    private DorderDao dorderDao;
    @Autowired
    private StockDao stockDao;

    @Override
    public int insert(AorderStorageDto aorderStorageDto) {
        String orderNo=String.valueOf(snowFlake.nextId());
        aorderStorageDto.setAllotNum("AS" + orderNo);
        int result = aorderDao.insertAorder(aorderStorageDto);
        for (int i = 0; i < aorderStorageDto.getAorderPutProds().length; i++) {
            AorderdetailsDto aorderPutProd = aorderStorageDto.getAorderPutProds()[i];
            aorderPutProd.setAorderAllotid(aorderStorageDto.getAllotId());

            Integer prodNum = aorderPutProd.getProdNum();
            Stock stock = aorderDao.queryBuerStockByParam(aorderPutProd);
            Integer u = prodNum + stock.getProdAwaitnum();
            int q = stock.getProdStocknum();
            Integer w = q - u;
            stock.setProdAwaitnum(u);
            stock.setProdResiduenum(w);
            aorderDao.updateAwaitNum(stock);
            System.out.println(stock);
        }
        aorderDao.insertAorderdetails(aorderStorageDto.getAorderPutProds());

        return result;
    }

    @Override
    public int insertBatch(BatchAllotDto batchAllotDto) {
        Integer stockId = batchAllotDto.getStockId();

        Stock stock = dorderDao.selectStockId(stockId);

        Integer prodNum = batchAllotDto.getProdNum();
//          Integer u = prodNum + stock.getProdAwaitnum();
        int q = stock.getProdStocknum();
        Integer w = q - prodNum;
        stock.setProdAwaitnum(prodNum);
        stock.setProdResiduenum(w);
        aorderDao.updateAwaitNum(stock);
        AorderdetailsDto aorderdetailsDto = new AorderdetailsDto();
        aorderdetailsDto.setProdCode(stock.getProdCode());
//        aorderdetailsDto.setAorderAllotid(stock.getAllotId());
        aorderdetailsDto.setProdName(stock.getProdName());
        aorderdetailsDto.setProdNum(batchAllotDto.getProdNum());
        aorderdetailsDto.setAorderAllotid(batchAllotDto.getAllotId());
        aorderdetailsDto.setProdImg(stock.getProdImg());
        aorderdetailsDto.setProdResiduenum(stock.getProdStocknum());
        aorderdetailsDto.setProdSpecial(stock.getProdSpecial());
        aorderdetailsDto.setProdPrice(stock.getProdPrice());
        aorderdetailsDto.setEmit(stock.getEmit());
        aorderdetailsDto.setProdArea(stock.getProdArea());
        aorderdetailsDto.setProdDepart(stock.getProdDepart());
        aorderdetailsDto.setCategoryTwoId(stock.getCategoryTwoId());
        aorderdetailsDto.setStockId(stock.getStockId());

        aorderDao.insertBatch(aorderdetailsDto);

        Integer allotId = batchAllotDto.getAllotId();
        AorderdetailsDto[] order = aorderDao.findAordertailsAll(allotId);

        for (int y = 0; y < order.length;y++){
            AorderdetailsDto outProdd = order[y];
            Stock stock1 = aorderDao.queryBuerStockByParam(outProdd);
            Integer prodNum1 = batchAllotDto.getProdNum();
            int u = prodNum1 + stock1.getProdAwaitnum();
            stock.setProdAwaitnum(u);
        }
        return  stockId;
    }

    @Override
    public int updateBatch(BatchAllotDto batchAllotDto) {
        Integer outId = batchAllotDto.getAllotId();
        AorderdetailsDto[] outProds = aorderDao.findAordertailsAll(outId);

        int result = 0;

        for (int i = 0; i < outProds.length; i++) {
            result = 1;
            AorderdetailsDto outProd = outProds[i];

            Integer prodNum = batchAllotDto.getProdNum();
            Stock stock = aorderDao.queryBuerStockByParam(outProd);
//            Integer u = prodNum + stock.getProdAwaitnum();
            int q = stock.getProdStocknum();
            Integer w = q - prodNum;
            stock.setProdAwaitnum(prodNum);
            stock.setProdResiduenum(w);
            outProd.setProdResiduenum(stock.getProdResiduenum());
            Integer prodResiduenum = outProd.getProdResiduenum();
            System.out.println(prodResiduenum);
            aorderDao.updateR(outProd);
            aorderDao.updateAwaitNum(stock);

            aorderDao.updateBatch(batchAllotDto);
        }
        Integer outIds = batchAllotDto.getAllotId();
        AorderdetailsDto[] order = aorderDao.findAordertailsAll(outIds);

        for (int q = 0; q < order.length;q++){

            AorderdetailsDto outProd = order[q];
            Stock stock = aorderDao.queryBuerStockByParam(outProd);
            Integer prodNum = outProd.getProdNum();
            int u = prodNum + stock.getProdAwaitnum();
            stock.setProdAwaitnum(u);
        }

        return result;
    }

    @Override
    public int updateOrder(AllotBatchDto allotBatchDto) {
        int update = aorderDao.update(allotBatchDto);

        return update;
    }

    @Override
    public int deleteBatch(BatchAllotDto batchAllotDto) {
        Stock stock1 = stockDao.get(batchAllotDto.getStockId());
        stock1.setProdAwaitnum(stock1.getProdAwaitnum()-batchAllotDto.getProdNum());
        stock1.setProdResiduenum(stock1.getProdStocknum()-stock1.getProdAwaitnum());
        int update = stockDao.update(stock1);
        if(update>0){
            aorderDao.deleteBatch(batchAllotDto);
        }

/*        Integer outId = batchAllotDto.getAllotId();
        AorderdetailsDto[] outProds = aorderDao.findAordertailsAll(outId);

        int result = 0;

        for (int i = 0; i < outProds.length; i++) {
            result = 1;
            AorderdetailsDto outProd = outProds[i];
            Stock stock = aorderDao.queryBuerStockByParam(outProd);
            Integer prodNum = outProd.getProdNum();
            Integer prodResiduenum = stock.getProdResiduenum();
            int o = prodNum + prodResiduenum;
            stock.setProdResiduenum(o);
            Integer prodAwaitnum = stock.getProdAwaitnum();
            int p = prodAwaitnum - prodNum;
            stock.setProdAwaitnum(p);
            aorderDao.updateAwaitNum(stock);
        }

        aorderDao.deleteBatch(batchAllotDto);*/
        return 1;
    }

    //根据订单表id查询入库信息
    @Override
    public List<AorderSelectDto> findAorderAllById (int allotId){
        List<AorderSelectDto> aorderAll = aorderDao.findAorderAll(allotId);
        if (aorderAll.size() > 0) {
            for (int i = 0; i < aorderAll.size(); i++) {
                List<AorderdetailsDto> all = aorderDao.findAorderdetailsAllById(aorderAll.get(i).getAllotId());
                if (all.size() > 0) {
                    aorderAll.get(i).setAorderPutProds(all);
                }
            }
        }

        return aorderAll;
    }

    @Override
    public PageInfo<AorderDto> selectByAll(Map<String, Object> map) {

        PageHelper.startPage(map); //开始起始页
        List<AorderDto> list = aorderDao.selectByAll(map); // 获取数据
        PageInfo<AorderDto> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }


    //查询所有调拨表信息
    @Override
    public List<AorderDto> findAorder() {
        List<AorderDto> aorderAll = aorderDao.findAorder();
        return aorderAll;
    }

    //根据订单表id删除入库信息
    @Transactional
    @Override
    public int deleteAorderById(int allotId) {

        AorderdetailsDto[] putProds = aorderDao.findAordertailsAll(allotId);

        int result = 0;
        for (int i = 0; i < putProds.length; i++) {
            result = 1;
            AorderdetailsDto putProd = putProds[i];

            //先查找prodArea,prodDepart,prodId
            Stock stock = aorderDao.queryBuerStockByParam(putProd);
            Integer prodNum = putProd.getProdNum();
            Integer prodResiduenum = stock.getProdResiduenum();
            int o = prodNum + prodResiduenum;
            stock.setProdResiduenum(o);
            Integer prodAwaitnum = stock.getProdAwaitnum();
            int p = prodAwaitnum - prodNum;
            stock.setProdAwaitnum(p);
            aorderDao.updateAwaitNum(stock);
        }
        aorderDao.deleteAorderById(allotId);
        return result;
    }

    //更新商品id
    @Transactional
    @Override
    public int updateAorderdetails(AorderStorageDto aorderStorageDto) {
        // 第一步先删除,此处需要前段讲putId的值传入过来
        Integer allotId = aorderStorageDto.getAllotId();
        aorderDao.deleteAorderByPutId(allotId);
        aorderDao.deleteAorderDetailsByPutId(allotId);
        // 第二部再插入
        //aorderStorageDto.setAllotStatus(1);
        int result = aorderDao.insertAorder(aorderStorageDto);
        for (int i = 0; i < aorderStorageDto.getAorderPutProds().length; i++) {
            AorderdetailsDto aorderPutProd = aorderStorageDto.getAorderPutProds()[i];
            aorderPutProd.setAorderAllotid(aorderStorageDto.getAllotId());

            Integer prodNum = aorderPutProd.getProdNum();
            Stock stock = aorderDao.queryBuerStockByParam(aorderPutProd);
            int q = stock.getProdStocknum();
            Integer w = q - prodNum;
            stock.setProdAwaitnum(prodNum);
            stock.setProdResiduenum(w);
            aorderDao.updateAwaitNum(stock);
            System.out.println(stock);
        }
        aorderDao.insertAorderdetails(aorderStorageDto.getAorderPutProds());
        return result;
    }

    //调拨表增加
    @Transactional
    @Override
    public int submitAddBuerStock(int allotId) {
        AorderdetailsDto[] putProds = aorderDao.findAordertailsAll(allotId);

        int result = 0;
        for (int i = 0; i < putProds.length; i++) {
            result = 1;
            AorderdetailsDto putProd = putProds[i];

            //先查找prodArea,prodDepart,prodId
            Stock stock = aorderDao.queryBuerStockByParam(putProd);

            //更新累加
            Integer prodNum = putProd.getProdNum();
            Integer newProdNum = stock.getProdStocknum() - prodNum;
            Integer stockId = stock.getStockId();
            stock.setProdStocknum(newProdNum);

            Integer prodAwaitnum = stock.getProdAwaitnum();
            int o = prodAwaitnum - prodNum;
            int q = stock.getProdStocknum();

            Integer w = q - o;
            stock.setProdAwaitnum(o);
            stock.setProdResiduenum(w);
            aorderDao.updateAwaitNum(stock);
            if (newProdNum == 0) {
                aorderDao.deleteStock(stockId);
            } else {
                aorderDao.updateAddSumStock(stock);

            }
            aorderDao.updateStatus(allotId);
        }

        return result;
    }

    @Override
    public PageInfo<AorderDto> getAord(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<AorderDto> list = aorderDao.getAord(map); // 获取数据
        PageInfo<AorderDto> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }

    @Override
    public PageInfo<AorderdetailsDto> getAorderdetails(Map<String, Object> map) {
        PageHelper.startPage(map); //开始起始页
        List<AorderdetailsDto> list = aorderDao.getAorderdetails(map); // 获取数据
        PageInfo<AorderdetailsDto> page = new PageInfo<>(list); // 实例化PageInfo
        return page;
    }
}
